﻿//------------
// TFS Linker
//(c)2009, TUM
//------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace TfsLinker
{
    public partial class MainForm : Form, ILogging
    {
        /// <summary>
        /// Used instance of the linker class.
        /// </summary>
        TfsLinker Linker;


        /// <summary>
        /// Form constructor.
        /// </summary>
        public MainForm()
        {
            //Init GUI
            InitializeComponent();

            //Init linker
            Linker = new TfsLinker(this);

            //Prepate GUI
            textBoxTfsAddress_TextChanged(null, null);

            //Write startup message.
            WriteLogEntry("TFS Linker started.");
        }

        /// <summary>
        /// Connect button has been clicked.
        /// </summary>
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            //De-signal workflow markers
            labelStep2.ForeColor = Color.Black;
            labelStep1.ForeColor = Color.Black;

            //Connect to server
            try
            {
                Linker.Connect(textBoxTfsAddress.Text);
            }
            catch (CriticalException c)
            {
                MessageBox.Show(this, c.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            catch (NonCriticalException)
            {
                labelStep1.ForeColor = Color.Red;
                return;
            }


            //Authenticate with server
            try
            {
                Linker.Authenticate();
            }
            catch (NonCriticalException)
            {
                labelStep1.ForeColor = Color.Red;
                return;
            }

            //Write step message to log
            WriteLogEntry("Getting projects ...");

            //Clear projects combo box and fill it with project names
            comboBoxProject.Items.Clear();
            foreach (ProjectStruct curProj in Linker.GetProjects())
            {
                //Insert project name and ID
                comboBoxProject.Items.Add(curProj);
            }

            //Everything went fine. We are connected. Signal next step and enable controls.
            labelStep3.ForeColor = Color.Red;
            comboBoxProject.Enabled = true;
            labelSelectProject.Enabled = true;

            WriteLogEntry("Connected.");
        }

        /// <summary>
        /// TFS server address text box has been edited.
        /// </summary>
        private void textBoxTfsAddress_TextChanged(object sender, EventArgs e)
        {
            //Change workflow markers
            buttonLink.Enabled = false;
            comboBoxProject.Enabled = false;
            comboBoxProject.Items.Clear();
            labelSelectProject.Enabled = false;

            //Check URI format
            try
            {
                Uri uri = new Uri(textBoxTfsAddress.Text, UriKind.Absolute);

                //Enable connect button and signal it
                buttonConnect.Enabled = true;

                labelStep2.ForeColor = Color.Red;
                labelStep1.ForeColor = Color.Red;
            }
            catch(UriFormatException)
            {
                //Invalid URI. Disable connect button
                buttonConnect.Enabled = false;

                labelStep1.ForeColor = Color.Red;
                labelStep2.ForeColor = Color.Black;
            }

            //Step 3 and 4 are not signaled
            labelStep3.ForeColor = Color.Black;
            labelStep4.ForeColor = Color.Black;
        }

        /// <summary>
        /// A project has been selected.
        /// </summary>
        private void comboBoxProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Enable link button and signal next step
            buttonLink.Enabled = true;
            labelStep4.ForeColor = Color.Red;
        }

        /// <summary>
        /// Link button has been clicked.
        /// </summary>
        private void buttonLink_Click(object sender, EventArgs e)
        {
            //De-signal workflow markers
            labelStep3.ForeColor = Color.Black;
            labelStep4.ForeColor = Color.Black;

            Linker.Link((ProjectStruct)comboBoxProject.SelectedItem);

            //Inform user about completed action
            WriteLogEntry("Completed.");
        }


        /// <summary>
        /// Writes a string to the log.
        /// </summary>
        /// <param name="entry">The message to be written.</param>
        public void WriteLogEntry(string entry)
        {
            listBoxLog.Items.Add(entry);
            listBoxLog.SelectedIndex = listBoxLog.Items.Count - 1;
        }
    }
}
